﻿Public Class Pagos
    Dim regreso As Boolean = False
    Dim dtDeudas As DataTable
    Dim credito As Double


    Dim tam As Integer = 25

    Public Function Mostrar(ByVal clie As String) As Boolean
        regreso = False
        dtDeudas = New DataTable
        dtDeudas = dsInfo.Tables("CUENTAS").Clone

        Dim deudas As DataRow() = dsInfo.Tables("CUENTAS").Select("CCLIE='" & clie & "'")
        For Each fila As DataRow In deudas
            Dim imp1 As Double = fila("SALDO")
            Dim imp2 As Object = dsData.Tables("PAGOS").Compute("SUM(IMPORTE)", "CCLIE='" & clie & "' AND REFER='" & fila("REFER") & "'")

            'Dim fPagos As DataRow() = dsData.Tables("PAGOS").Select("CCLIE='" & clie & "' AND REFER='" & fila("REFER") & "'")
            If (imp2 Is DBNull.Value) Then 'OrElse (imp1 - imp2 > 0) Then
                dtDeudas.ImportRow(fila)
            ElseIf (imp1 - imp2 > 0) Then
                Dim sPrevio As Double = fila("SALDO")
                'fila("SPREVIO") = fila("SALDO")
                fila("SALDO") = imp1 - imp2
                dtDeudas.ImportRow(fila)
                fila("SALDO") = sPrevio
            End If
        Next

        dtDeudas.Columns.Add("PAGADO", GetType(Double))
        'dtDeudas.Columns.Add("SPREVIO", GetType(Double))
        dtDeudas.Columns.Add("P", GetType(Image))
        dtDeudas.Columns.Add("T", GetType(Image))

        If dtDeudas.Rows.Count = 0 Then Return True


        Dim bmp As New Bitmap(My.Resources.busy)
        Comprime_Imagen(bmp, tam, tam)

        Dim bmp2 As New Bitmap(My.Resources.communication)
        Comprime_Imagen(bmp2, tam, tam)

        For Each fila As DataRow In dtDeudas.Rows
            'fila("PAGADO") = False
            fila("P") = New Bitmap(bmp2)
            fila("T") = New Bitmap(bmp)
        Next

        dgDeudas.DataSource = dtDeudas
        'dgDeudas.Columns("SPREVIO").Visible = False

        For Each col As DataColumn In dtDeudas.Columns
            If col.ExtendedProperties.Contains("formato") Then
                dgDeudas.Columns(col.ColumnName).DefaultCellStyle.Format = col.ExtendedProperties("formato")
                dgDeudas.Columns(col.ColumnName).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                dgDeudas.Columns("PAGADO").DefaultCellStyle.Format = "$ ###,###,##0.00"
            End If
        Next
        'dgDeudas.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
        dgDeudas.Columns("PAGADO").Visible = True
        dgDeudas.Columns(0).Visible = False

        credito = CType(dsInfo.Tables("CLIENTES").Select("CCLIE='" & clie & "'").GetValue(0), DataRow)("LIM_CRED")
        Dim ventas As Object = dsData.Tables("FACTURAS").Compute("SUM(TOTAL)", "CCLIE='" & clie & "' AND TIPO='C'")
        If ventas Is DBNull.Value Then ventas = 0
        credito -= ventas

        If credito = 0 Then
            lbDisp.Visible = False
            lbDisponible.Visible = False
        Else
            lbDisp.Visible = True
            lbDisponible.Visible = True
        End If

        calcula()

        btnPagar.Enabled = False

        Me.ShowDialog()

        'For Each fila As DataRow In dtDeudas.Rows
        '    fila("SALDO") = fila("SPREVIO")
        'Next

        Return regreso
    End Function

    Private Sub Pagos_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Select Case e.KeyCode
            Case Keys.F1
                btnPagar_Click(Nothing, Nothing)
            Case Keys.F9
                btnTodo_Click(Nothing, Nothing)
        End Select
    End Sub

    Private Sub btnTodo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTodo.Click
        For Each fila As DataRow In dtDeudas.Rows
            fila("PAGADO") = fila("SALDO")
        Next
        calcula()
    End Sub

    Private Sub btnPagar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPagar.Click
        If btnPagar.Enabled Then
            Dim cant As Integer = 0

            For Each fila As DataRow In dtDeudas.Rows
                If fila("PAGADO") Is DBNull.Value Then
                    fila("PAGADO") = 0
                End If
            Next

            For Each fila As DataRow In dtDeudas.Select("PAGADO>0")
                Dim fPag As DataRow = dsData.Tables("PAGOS").NewRow
                fPag("CCLIE") = fila("CCLIE")
                fPag("REFER") = fila("REFER")
                fPag("IMPORTE") = fila("PAGADO")
                fPag("FECHA") = Format(Now, "dd/MM/yyyy")
                fPag("USUARIO") = Valor_Config("EMPRESA", "ID del Vendedor")
                fPag("TIPO") = "C"
                dsData.Tables("PAGOS").Rows.Add(fPag)
                cant += 1
            Next

            Guarda_Datos()
            If cant > 0 Then
                regreso = True
            Else
                regreso = False
            End If
            Me.Close()
        End If
    End Sub

    Private Sub Pagos_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        Define_Ancho
    End Sub

    Private Sub dgDeudas_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgDeudas.CellContentClick
        If e.ColumnIndex = dgDeudas.Columns.Count - 1 Then
            If Not dtDeudas.Rows(e.RowIndex)("PAGADO") Is DBNull.Value Then
                dtDeudas.Rows(e.RowIndex)("PAGADO") = DBNull.Value
            Else
                dtDeudas.Rows(e.RowIndex)("PAGADO") = dtDeudas.Rows(e.RowIndex)("SALDO")
            End If
            calcula()
        ElseIf e.ColumnIndex = dgDeudas.Columns.Count - 2 Then
            Dim monto As Integer = Val(num.Mostrar(, True, "Escribe el monto"))
            If monto > 0 Then
                If monto > dtDeudas.Rows(e.RowIndex)("SALDO") Then
                    msg.Mostrar("El monto excede el saldo del documento", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Exit Sub
                End If
                dtDeudas.Rows(e.RowIndex)("PAGADO") = monto
            Else
                dtDeudas.Rows(e.RowIndex)("PAGADO") = DBNull.Value
            End If
            Define_Ancho()
            calcula()
        End If
    End Sub

    Private Sub Define_Ancho()
        Dim ancho As Integer = 0
        For Each col As DataGridViewColumn In dgDeudas.Columns
            ancho += col.Width + 3
        Next
        Me.Width = ancho + 24
    End Sub

    Private Sub calcula()
        Dim pagar As Double = 0
        Dim saldo As Double = 0

        Dim bmp As New Bitmap(My.Resources.check)
        Comprime_Imagen(bmp, tam, tam)

        Dim bmp2 As New Bitmap(My.Resources.busy)
        Comprime_Imagen(bmp2, tam, tam)

        For Each fila As DataRow In dtDeudas.Rows
            saldo += fila("SALDO")
            If Not fila("PAGADO") Is DBNull.Value Then
                pagar += fila("PAGADO")
                If fila("PAGADO") = fila("SALDO") Then
                    fila("T") = bmp
                Else
                    fila("T") = bmp2
                End If
            Else
                fila("T") = bmp2
            End If
        Next

        If credito > 0 Then
            lbDisponible.Text = Format(credito - saldo + pagar, "$ ###,###,##0.00")
        End If


        If pagar = 0 Then
            lbPagar.Text = ""
            btnPagar.Enabled = False
        Else
            lbPagar.Text = Format(pagar, "$ ###,###,##0.00")
            btnPagar.Enabled = True
        End If

    End Sub
End Class